<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
	<title>Getting Started Part3</title>
  <link rel="stylesheet" type="text/css" href="documentation.css" />
</head>
<body>

<h1 id="getting-started-part-3">Getting started - part 3</h1>

<p>This is the third part of a series. Since the text refers to a running example, you should start with the first two parts.</p>

<h2 id="managing-dependencies">Managing Dependencies</h2>

<p>So far, we have used global variables to hold the access to our model component. As I mentioned already at the beginning, this is not a good practise. Konstrukt is designed in a way so that it is possible to use a <a href="http://www.picocontainer.org/patterns.html">dependency injection container</a> for managing shared components in the application. We'll now see how the application can be transformed to use this.</p>

<p>For this purpose we'll use the simplistic <a href="http://github.com/troelskn/bucket">Bucket</a> library. If you prefer a different container, it's trivial to change, since such containers - by their nature - introduce very few dependencies.</p>

<p>You can install Bucket in a shared place on your machine, but for this tutorial, we'll just pull it in to the application we're working on. Open a shell, go to your site's root folder (<code>/var/www/foo</code>) and type the following commands:</p>

<pre class="shell">mkdir thirdparty
git clone git@github.com:troelskn/bucket.git thirdparty/bucket</pre>

<p>This will pull a copy of Bucket into the folder <code>thirdparty/bucket</code>.</p>

<p>Before we can make use of Bucket, we need to configure our project to use it. Open up <code>config/development.inc.php</code> and replace the contents with:</p>

<pre class="php"><span class="kw2">&lt;?php</span>
<span class="kw1">require_once</span> <span class="st0">'applicationfactory.php'</span>;
<span class="kw1">require_once</span> <span class="kw3">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span>.<span class="st0">'/../thirdparty/bucket/lib/bucket.inc.php'</span>;
date_default_timezone_set<span class="br0">&#40;</span><span class="st0">'Europe/Paris'</span><span class="br0">&#41;</span>;
<span class="kw2">function</span> create_container<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="re0">$factory</span> = <span class="kw2">new</span> ApplicationFactory<span class="br0">&#40;</span><span class="br0">&#41;</span>;
  <span class="re0">$container</span> = <span class="kw2">new</span> bucket_Container<span class="br0">&#40;</span><span class="re0">$factory</span><span class="br0">&#41;</span>;
  <span class="re0">$factory</span>-&gt;<span class="me1">pdo_dsn</span> = <span class="st0">&quot;sqlite:&quot;</span> . <span class="kw3">dirname</span><span class="br0">&#40;</span><span class="kw3">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span><span class="br0">&#41;</span> . <span class="st0">&quot;/var/database.sqlite&quot;</span>;
  <span class="re0">$factory</span>-&gt;<span class="me1">pdo_attributes</span> = <span class="kw3">array</span><span class="br0">&#40;</span>PDO::<span class="me2">ATTR_ERRMODE</span> =&gt; PDO::<span class="me2">ERRMODE_EXCEPTION</span><span class="br0">&#41;</span>;
  <span class="kw1">return</span> <span class="re0">$container</span>;
<span class="br0">&#125;</span></pre>

<p>The function <code>create_container</code> creates a Bucket container and attaches a factory (<code>ApplicationFactory</code>) for creating objects. We need a <code>ApplicationFactory</code> to go with this - Open a new file at <code>lib/applicationfactory.php</code> and enter:</p>

<pre class="php"><span class="kw2">&lt;?php</span>
<span class="coMULTI">/**
  * Provides class dependency wiring for this application
  */</span>
<span class="kw2">class</span> ApplicationFactory <span class="br0">&#123;</span>
  <span class="kw2">public</span> <span class="re0">$pdo_dsn</span>;
  <span class="kw2">public</span> <span class="re0">$pdo_username</span>;
  <span class="kw2">public</span> <span class="re0">$pdo_password</span>;
  <span class="kw2">public</span> <span class="re0">$pdo_attributes</span> = <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
  <span class="kw2">function</span> new_PDO<span class="br0">&#40;</span><span class="re0">$c</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> <span class="kw2">new</span> PDO<span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">pdo_dsn</span>, <span class="re0">$this</span>-&gt;<span class="me1">pdo_username</span>, <span class="re0">$this</span>-&gt;<span class="me1">pdo_password</span>, <span class="re0">$this</span>-&gt;<span class="me1">pdo_attributes</span><span class="br0">&#41;</span>;
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre>

<p>And finally, open up <code>www/index.php</code> and change it so that it contains:</p>

<pre class="php"><span class="kw2">&lt;?php</span>
<span class="kw1">require_once</span> <span class="kw3">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> . <span class="st0">'/../config/global.inc.php'</span>;
k<span class="br0">&#40;</span><span class="br0">&#41;</span>
  <span class="co1">// Use container for wiring of components</span>
  -&gt;<span class="me1">setComponentCreator</span><span class="br0">&#40;</span><span class="kw2">new</span> k_InjectorAdapter<span class="br0">&#40;</span>create_container<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
  <span class="co1">// Dispatch request</span>
  -&gt;<span class="me1">run</span><span class="br0">&#40;</span><span class="st0">'components_Root'</span><span class="br0">&#41;</span>
  -&gt;<span class="me1">out</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</pre>

<p>At this point, the dependency injection container is set up and configured, and all components will have their dependencies automagically filled at instantiation.</p>

<p>We can now edit the components to take advantage of this. Open up <code>lib/components/contacts/list.php</code> and add a constructor:</p>

<pre class="php"><span class="kw2">class</span> components_contacts_List <span class="kw2">extends</span> k_Component <span class="br0">&#123;</span>
  <span class="kw2">protected</span> <span class="re0">$contacts</span>;
  <span class="kw2">function</span> __construct<span class="br0">&#40;</span>ContactsGateway <span class="re0">$contacts</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$this</span>-&gt;<span class="me1">contacts</span> = <span class="re0">$contacts</span>;
  <span class="br0">&#125;</span>
  ...</pre>

<p>The container will see that <code>components_contacts_List</code> requires an instance of <code>ContactsGateway</code> and provide it. We can then replace the references to the global variable, as so:</p>

<pre class="php"><span class="kw2">class</span> components_contacts_List <span class="kw2">extends</span> k_Component <span class="br0">&#123;</span>
  ...
  <span class="kw2">function</span> renderHtml<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$this</span>-&gt;<span class="me1">document</span>-&gt;<span class="me1">setTitle</span><span class="br0">&#40;</span><span class="st0">&quot;Contacts&quot;</span><span class="br0">&#41;</span>;
    <span class="re0">$t</span> = <span class="kw2">new</span> k_Template<span class="br0">&#40;</span><span class="st0">&quot;templates/contacts-list.tpl.php&quot;</span><span class="br0">&#41;</span>;
    <span class="kw1">return</span> <span class="re0">$t</span>-&gt;<span class="me1">render</span><span class="br0">&#40;</span>
      <span class="re0">$this</span>,
      <span class="kw3">array</span><span class="br0">&#40;</span>
        <span class="st0">'contacts'</span> =&gt; <span class="re0">$this</span>-&gt;<span class="me1">contacts</span>-&gt;<span class="me1">all</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
  <span class="br0">&#125;</span>
  ...</pre>

<p>We have to do the same thing with <code>lib/components/contacts/entity.php</code></p>

<pre class="php"><span class="kw2">class</span> components_contacts_Entity <span class="kw2">extends</span> k_Component <span class="br0">&#123;</span>
  <span class="kw2">protected</span> <span class="re0">$contact</span>;
  <span class="kw2">protected</span> <span class="re0">$contacts</span>;
  <span class="kw2">function</span> __construct<span class="br0">&#40;</span>ContactsGateway <span class="re0">$contacts</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$this</span>-&gt;<span class="me1">contacts</span> = <span class="re0">$contacts</span>;
  <span class="br0">&#125;</span>
  <span class="kw2">function</span> dispatch<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$this</span>-&gt;<span class="me1">contact</span> = <span class="re0">$this</span>-&gt;<span class="me1">contacts</span>-&gt;<span class="me1">fetchByName</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">name</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="re0">$this</span>-&gt;<span class="me1">contact</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      <span class="kw1">throw</span> <span class="kw2">new</span> k_PageNotFound<span class="br0">&#40;</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>
    <span class="kw1">return</span> <span class="kw2">parent</span>::<span class="me2">dispatch</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
  <span class="br0">&#125;</span>
  ...
  <span class="kw2">function</span> process<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    ...
      try <span class="br0">&#123;</span>
        <span class="re0">$this</span>-&gt;<span class="me1">contacts</span>-&gt;<span class="me1">save</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">contact</span><span class="br0">&#41;</span>;
      <span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span>Exception <span class="re0">$ex</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        @<span class="re0">$this</span>-&gt;<span class="me1">contact</span>-&gt;<span class="me1">errors</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$ex</span>-&gt;<span class="me1">getMessage</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
        <span class="kw1">return</span> <span class="kw2">false</span>;
      <span class="br0">&#125;</span>
    ...</pre>

<p>You can now go verify that the application works exactly as before. That may seem like a lot of effort, but the result is that all components are now managed by the dependency injection container. <em>Why</em> this is a big deal is a bit beyond the scope of this tutorial, so I suggest <a href="http://stackoverflow.com/search?q=dependency+injection">other sources</a> for that.</p>

<h3 id="the-default-starterpack">The default starterpack</h3>

<p>A lot of the bootstrapping that we did above would need to be done for each new application. That is why there is a starterpack which has this and more preconfigured. Instead of using <code>starterpack_light</code>, the <code>starterpack_default</code> contains a more complete setup, including Bucket.</p>

<p>Before we continue, we'll just pull in the remaining bits from <code>starterpack_default</code>:</p>

<pre class="shell">svn export http://konstrukt.googlecode.com/svn/trunk/examples/starterpack_default/script script
svn export http://konstrukt.googlecode.com/svn/trunk/examples/starterpack_default/test test</pre>

<p>This "upgrades" our <code>starterpack_light</code>, so that our application is now similar to how it would have looked if we started out with <code>starterpack_default</code> (Except for the bootstrap, which we'll cover later). The folder <code>script/</code> contains various commandline scripts for maintenance and code generation.</p>

<h2 id="testing">Testing</h2>

<p>Noticed that last dir we pulled in? (Hint: It begins with <em>t</em> and ends with <em>est</em>). Yup, the default Konstrukt configuration holds tests in a dir called <code>test/</code>. This is further split into a folder for unit tests <code>test/unit/</code> and a folder for functional tests <code>test/functional/</code>. Unit tests test the smallest "unit" of your appplication - Typically a single class. Functional tests test interaction. In Konstrukt, you would typically have a test per component.</p>

<p>The starterpack comes with a single functional test to start out with. You can try running it with:</p>

<pre class="shell">php test/functional/root.test.php</pre>

<p>Hopefully it passes.</p>

<p>This first test was for free, but we should want to write tests for our own components. Time for another freebie:</p>

<pre class="shell">script/generate_test_functional.php components_contacts_List</pre>

<p>As you might suspect, this generates a functional test case for the component <code>components_contacts_List</code>. Or rather, it generates the file and skeleton - You still need to fill the actual tests in manually. Open up the newly generated file at: <code>test/functional/contacts_list.test.php</code>. As you can see, it contains a <a href="http://www.simpletest.org/en/web_tester_documentation.html">web testcase</a> for the List component.</p>

<p>Unlike regular web testcases from SimpleTest, this one uses a <code>k_VirtualSimpleBrowser</code>. This is a component provided by Konstrukt which completely mocks out the HTTP-access, thus making it possible to test a component without running it through a web server. Not only is this faster and simpler, but it makes it possible to mock out dependencies - Something which isn't really possible with regular web testcases.</p>

<p>The skeleton contains a single test, which simply checks that the component responds to a GET request. Let's add a more interesting test to it:</p>

<pre class="php"><span class="kw2">function</span> test_lists_contacts<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="re0">$this</span>-&gt;<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'/'</span><span class="br0">&#41;</span>;
  <span class="re0">$this</span>-&gt;<span class="me1">assertLink</span><span class="br0">&#40;</span><span class="st0">&quot;Jabba the Hutt&quot;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span></pre>

<p>If you didn't mess too much around with the data, this should pass. However, letting tests depend on the state of the database is a bad idea. Let's mock out that dependency, shall we:</p>

<pre class="php"><span class="kw2">function</span> test_lists_contacts<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  Mock::<span class="me2">Generate</span><span class="br0">&#40;</span><span class="st0">'ContactsGateway'</span><span class="br0">&#41;</span>;
  <span class="re0">$contacts</span> = <span class="kw2">new</span> MockContactsGateway<span class="br0">&#40;</span><span class="br0">&#41;</span>;
  <span class="re0">$jabba</span> = <span class="kw2">new</span> Contact<span class="br0">&#40;</span><span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">'first_name'</span> =&gt; <span class="st0">&quot;Jabba&quot;</span>, <span class="st0">'last_name'</span> =&gt; <span class="st0">&quot;the Hutt&quot;</span>, <span class="st0">'short_name'</span> =&gt; <span class="st0">&quot;jabba&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
  <span class="re0">$contacts</span>-&gt;<span class="me1">setReturnValue</span><span class="br0">&#40;</span><span class="st0">'all'</span>, <span class="kw3">array</span><span class="br0">&#40;</span><span class="re0">$jabba</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
  <span class="re0">$this</span>-&gt;<span class="me1">container</span>-&gt;<span class="me1">set</span><span class="br0">&#40;</span><span class="re0">$contacts</span>, <span class="st0">'ContactsGateway'</span><span class="br0">&#41;</span>;
  <span class="re0">$this</span>-&gt;<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'/'</span><span class="br0">&#41;</span>;
  <span class="re0">$this</span>-&gt;<span class="me1">assertLink</span><span class="br0">&#40;</span><span class="st0">&quot;Jabba the Hutt&quot;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span></pre>

<p>First we generate a <a href="http://www.simpletest.org/en/mock_objects_documentation.html">mock</a> for the model component <code>ContactsGateway</code> and set up its expectations. Then we configure the containier to use our mocked object in place of the real deal. When running the test, the container will assign the mock object to the list component, thus making the test independent of the database.</p>

<p>This isn't a tutorial in writing automated tests, so we'll leave off here, but hopefully this shows the basic framework that Konstrukt provides for functional tests.</p>

<h2 id="debugging-logs">Debugging + logs</h2>

<p>The inner workings of Konstrukt's dispatch process can be a bit daunting at first. To help understanding how each request is processed, you can turn on debug logging. This will log information about each component in the chain of execution. If you use the default starterpack, logging is enabled by default. Otherwise you can add it with a single line to the bootstrap (<code>www/index.php</code>):</p>

<pre class="php">k<span class="br0">&#40;</span><span class="br0">&#41;</span>
  ...
  -&gt;<span class="me1">setLog</span><span class="br0">&#40;</span><span class="kw3">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> . <span class="st0">'/../log/debug.log'</span><span class="br0">&#41;</span>
  ...</pre>

<p>You can follow the output as it flows into the log by running the <code>tail</code> command:</p>

<pre class="shell">tail -f log/debug.log</pre>

<p>Which will give you something like the following:</p>

<pre class="lisp"><span class="br0">&#40;</span><span class="kw1">request</span>
  <span class="br0">&#40;</span>time <span class="st0">&quot;2009-10-18 23:15:46&quot;</span><span class="br0">&#41;</span>
  <span class="br0">&#40;</span>method <span class="st0">&quot;get&quot;</span><span class="br0">&#41;</span>
  <span class="br0">&#40;</span>request-uri <span class="st0">&quot;/foo/www/contacts&quot;</span><span class="br0">&#41;</span>
  <span class="br0">&#40;</span>headers <span class="br0">&#40;</span><span class="kw1">array</span> <span class="br0">&#40;</span>length <span class="nu0">9</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>host <span class="br0">&#40;</span><span class="kw1">string</span> <span class="st0">&quot;localhost&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>user-agent <span class="br0">&#40;</span><span class="kw1">string</span> <span class="st0">&quot;Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.14) Gecko/2009090216 Ubuntu/9.04 (jaunty) Firefox/3.0.14&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>accept <span class="br0">&#40;</span><span class="kw1">string</span> <span class="st0">&quot;text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>accept-language <span class="br0">&#40;</span><span class="kw1">string</span> <span class="st0">&quot;en-us,en;q=0.5&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>accept-encoding <span class="br0">&#40;</span><span class="kw1">string</span> <span class="st0">&quot;gzip,deflate&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>accept-charset <span class="br0">&#40;</span><span class="kw1">string</span> <span class="st0">&quot;ISO-8859-1,utf-8;q=0.7,*;q=0.7&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>keep-alive <span class="br0">&#40;</span><span class="kw1">string</span> <span class="st0">&quot;300&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>connection <span class="br0">&#40;</span><span class="kw1">string</span> <span class="st0">&quot;keep-alive&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>cookie <span class="br0">&#40;</span><span class="kw1">string</span> <span class="st0">&quot;wp-settings-time-1=1234546590; re_ret=0&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
<span class="br0">&#40;</span><span class="kw1">dispatch</span>
  <span class="br0">&#40;</span>class-name <span class="st0">&quot;components_Root&quot;</span> <span class="br0">&#40;</span>defined-in <span class="st0">&quot;/var/www/foo/lib/components/root.php&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
  <span class="br0">&#40;</span>name <span class="kw1">*NULL*</span><span class="br0">&#41;</span>
  <span class="br0">&#40;</span>next contacts<span class="br0">&#41;</span><span class="br0">&#41;</span>
<span class="br0">&#40;</span><span class="kw1">dispatch</span>
  <span class="br0">&#40;</span>class-name <span class="st0">&quot;components_contacts_List&quot;</span> <span class="br0">&#40;</span>defined-in <span class="st0">&quot;/var/www/foo/lib/components/contacts/list.php&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
  <span class="br0">&#40;</span>name contacts<span class="br0">&#41;</span>
  <span class="br0">&#40;</span>next <span class="kw1">*NULL*</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
<span class="br0">&#40;</span><span class="kw1">http-response</span>
  <span class="br0">&#40;</span>http-status <span class="nu0">200</span><span class="br0">&#41;</span>
  <span class="br0">&#40;</span>content-type <span class="st0">&quot;text/html&quot;</span><span class="br0">&#41;</span>
  <span class="br0">&#40;</span>charset <span class="st0">&quot;utf-8&quot;</span><span class="br0">&#41;</span>
  <span class="br0">&#40;</span>headers<span class="br0">&#41;</span><span class="br0">&#41;</span></pre>

<p>There are three types of entries in that piece of log. The <code>request</code> entry logs the parsed http-request.</p>

<p>Following that comes a number of <code>dispatch</code> entries - one for each component that gets invoked on the chain. There will always be the root component at the beginning, but there can be any number of components following that. For each component is logged the class name of the component along with the url-name that it has and the next name on the chain.</p>

<p>Finally there is a <code>response</code> entry which displays the value of the http-response that is sent back to the client.</p>

<p>You can further send custom log messages to the debug log, from within a component, by calling to <code>$this-&gt;debug()</code>. This is often used during development, instead of <code>var_dump</code>.</p>

<p class="note">Note: The format of the debug log is <a href="http://en.wikipedia.org/wiki/S-expression">s-expressions</a>. You can syntax-highlight it as lisp code. For example, if you have <code>source-highlight</code> installed on your system, you can view the log with the command <code>tail -f log/debug.log | source-highlight --out-format=esc --src-lang=slang</code>.</p>

<p>If you're not so fond of the command line, you might want to use the in-browser log output instead. Go back to the bootstrap and add the following line:</p>

<pre class="php">k<span class="br0">&#40;</span><span class="br0">&#41;</span>
  ...
  -&gt;<span class="me1">setDebug</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
  ...</pre>

<p>If you view the application in your browser, you'll notice a green box in the top-right corner. Try clicking on it and you'll get a neatly formatted table with the same information as you have in the log-file.</p>

<h2 id="error-log">Error log</h2>

<p>In addition to the debug log, the default setup configures Apache to put errors in a custom error log. This will happen if Apache is misconfigured or if a fatal error happens in your php application. If you are having trouble getting things to work, try checking if there is anything in <code>log/error.log</code>.</p>

<h2 id="configuring-application-for-production">Configuring application for production</h2>

<p>On a production site, you obviously don't want errors displayed in the browser or a lot of dispatching information in the debug log. You could edit the bootstrap and remove remove or comment out the calls to <code>setLog</code> and <code>setDebug</code> after you have deployed to the production site, but this is a bit tedious and error-prone. Some times you also want to make other configuration changes in the various sites. For example, you probably need different connection details to the database (if you use one) and so on.</p>

<p>You can put different global configuration in a config file under <code>config/</code>. The bootstrap will include <code>global.inc.php</code>, which in turn will load either <code>local.inc.php</code> or fall back to <code>development.inc.php</code>.</p>

<p>The idea is that you create a separate config file for each target site (development, staging, production etc.) and name them accordingly (<code>development.inc.php</code>, <code>staging.inc.php</code>, <code>production.inc.php</code> etc.). On each site, you can create a symlink that points to the relevant config file. For example, on the production site, you would create a link <code>local.inc.php -&gt; production.inc.php</code>. You can make this part of your deployment script, if you use an automated deployment script.</p>

<p>To make the debug/logging configurable, we'll have to make a small change to <code>index.php</code>:</p>

<pre class="php"><span class="kw2">&lt;?php</span>
<span class="kw1">require_once</span> <span class="kw3">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> . <span class="st0">'/../config/global.inc.php'</span>;
k<span class="br0">&#40;</span><span class="br0">&#41;</span>
  -&gt;<span class="me1">setComponentCreator</span><span class="br0">&#40;</span><span class="kw2">new</span> k_InjectorAdapter<span class="br0">&#40;</span>create_container<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
  -&gt;<span class="me1">setLog</span><span class="br0">&#40;</span><span class="re0">$debug_log_path</span><span class="br0">&#41;</span>
  -&gt;<span class="me1">setDebug</span><span class="br0">&#40;</span><span class="re0">$debug_enabled</span><span class="br0">&#41;</span>
  -&gt;<span class="me1">run</span><span class="br0">&#40;</span><span class="st0">'components_Root'</span><span class="br0">&#41;</span>
  -&gt;<span class="me1">out</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</pre>

<p>And to <code>development.inc.php</code> we add the two configuration parameters:</p>

<pre class="php">...
<span class="re0">$debug_log_path</span> = <span class="kw3">dirname</span><span class="br0">&#40;</span><span class="kw3">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span><span class="br0">&#41;</span> . <span class="st0">&quot;/var/debug.log&quot;</span>;
<span class="re0">$debug_enabled</span> = <span class="kw2">true</span>;
...</pre>

<p>Now, let's assume you wanted a production site (which isn't too far fetched, I hope). Start by making a copy of development:</p>

<pre class="shell">cp config/development.inc.php config/production.inc.php</pre>

<p>And edit it to suit your needs. For now, we'll just disable debug logging and in-browser debugging. Replace the two lines from before with:</p>

<pre class="php">...
<span class="re0">$debug_log_path</span> = <span class="kw2">null</span>;
<span class="re0">$debug_enabled</span> = <span class="kw2">false</span>;
...</pre>

<p>You can now switch to use the production site profile by making a symlink:</p>

<pre class="shell">ln -s config/production.inc.php config/local.inc.php</pre>

<p>Without the link, the development profile is loaded. So for your development site, you won't have any link.</p>

<p>The actual deployment process is a bit outside the scope of Konstrukt and this tutorial, but you can use tools such as <a href="http://www.capify.org/">Capistrano</a> or <a href="http://phing.info/">Phing</a> for this. Or you can just use a bunch of shell scripts.</p>

<p class="note">Note: The default starterpack already has a bootstrap which is configured in this way, so if you start from there, all you need to do is make a copy of <code>development.inc.php</code> for the production (and any other) site and put a link when you deploy to it.
</p>
</body>
</html>
